package com.mdlib.helper.module.account.fragments;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ljlib.core.util.common.StringUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.AccountModel;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.ui.UIHelper;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by librabin on 16/11/24.
 */

public class SetPwdFragment extends BaseTitleFragment {


    @Bind(R.id.tv_set_phone)
    TextView mTvSetPhone;
    @Bind(R.id.et_set_pass)
    EditText mEtSetPass;
    @Bind(R.id.et_set_pass_again)
    EditText mEtSetPassAgain;
    @Bind(R.id.tv_set_ok)
    TextView mTvSetOk;
    private String phone;
    private String code;

    public static SetPwdFragment newInstance(String phone, String code) {
        SetPwdFragment setPassFragment = new SetPwdFragment();
        Bundle bundle = new Bundle();
        bundle.putString(UIHelper.PHONE, phone);
        bundle.putString(UIHelper.CODE, code);
        setPassFragment.setArguments(bundle);
        return setPassFragment;
    }


    @Override
    protected void initBundle(Bundle bundle) {
        super.initBundle(bundle);

        phone = bundle.getString(UIHelper.PHONE);
        code = bundle.getString(UIHelper.CODE);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_set_password;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.set_pass_tv_title, R.color.white)
                .setTitleBgColor(R.color.myred)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });

        mTvSetPhone.setText(phone);
    }

    @OnClick(R.id.tv_set_ok)
    public void onClick() {
        String pwd = mEtSetPass.getText().toString();
        String aginPwd = mEtSetPassAgain.getText().toString();
        if (StringUtils.isEmpty(pwd)) {
            ToastUtil.showToast("输入框不能为空！");
            return;
        }
        if (pwd.equals(aginPwd)) {
            setBindPhone(pwd);
        } else {
            ToastUtil.showToast("两次输入的密码不一致！");
        }
    }


    /**
     * 绑定手机号
     *
     * @param pwd
     */
    private void setBindPhone(String pwd) {
        addSubscribe(DataManager.getInstance().getAccountApi()
                .bindPhone(AccountModel.getInstance().getThirdUid(), phone, pwd, code)
                .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                .subscribe(new RxSubscribe<BaseResult>() {
                    @Override
                    protected void onSucc(BaseResult baseResult) {
                        addFragment(BindPhoneSuccFragment.newInstance(phone));

                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.showToast(msg);
                    }
                }));

    }
}
